package com.wd.elec.utils;

import com.alibaba.fastjson2.JSONArray;
import com.wd.elec.common.enums.PowerUnitCategory;
import com.wd.elec.domain.CnNodeData;
import com.wd.elec.domain.PowerUnitData;
import com.wd.elec.domain.vo.NodeDataTimeline;
import com.wd.elec.domain.vo.PowerUnitStack;

import java.util.*;

/**
 * @className: DataUtil
 * @description: TODO 类描述
 * @author: liumingkun
 * @date: 2024/9/6
 **/
public class DataUtil {


    public static List<PowerUnitStack> parseStack(List<PowerUnitData> dataList) {
        Map<String, Map<String, List<Float>>> map = new HashMap<>();
        for (int i = 0; i < dataList.size(); i++) {
            PowerUnitData powerUnitData = dataList.get(i);
            String data = powerUnitData.getData();
            List<Float> floats = strToListFloat(data);
            for (int j = 0; j < floats.size(); j++) {
                Float aFloat = floats.get(j);
                String time = DateTimeUtil.getTime(j);

                //获取该时间点的数据
                Map<String, List<Float>> stringListMap = map.get(time);

                if (stringListMap == null) {
                    stringListMap = new HashMap<>();
                    map.put(time, stringListMap);
                }


                String category = PowerUnitCategory.fromValue(powerUnitData.getCategory().intValue()).toString();

                List<Float> valueList = stringListMap.get(category);
                if (valueList == null) {
                    valueList = new ArrayList<>();
                    stringListMap.put(category, valueList);
                }

                valueList.add(aFloat);

            }
        }

        List<PowerUnitStack> powerUnitStacks = parseToDataStack(map);

        Collections.sort(powerUnitStacks, Comparator.comparing(PowerUnitStack::getDt));
        return powerUnitStacks;
    }


    public static List<PowerUnitStack> parseToDataStack(Map<String, Map<String, List<Float>>> map) {
        List<PowerUnitStack> list = new ArrayList<>();
        for (String key : map.keySet()) {
            PowerUnitStack powerUnitStack = new PowerUnitStack();
            powerUnitStack.setDt(key);
            Map<String, List<Float>> stringListMap = map.get(key);
            Map<String, Float> puVlaue = new HashMap<>();
            for (String vKey : stringListMap.keySet()
            ) {
                puVlaue.put(vKey, avgFloatList(stringListMap.get(vKey)));
            }
            powerUnitStack.setValue(puVlaue);
            list.add(powerUnitStack);
        }
        return list;
    }

    public static Float avgFloatList(List<Float> list) {
        Float average = (float) list.stream()
                .mapToDouble(Float::doubleValue) // 转换为 DoubleStream
                .average() // 计算平均值
                .orElse(0.0); // 如果列表为空，返回 0.0

        return average;
    }

    public static Float sumFloatList(List<Float> list) {
        Float sum = (float) list.stream()
                .mapToDouble(Float::doubleValue) // 转换为 DoubleStream
                .sum(); //求和
        return sum;
    }

    public static List<Float> disSumFloatList(List<List<Float>> floatList) {
        List<Float> resultList = new ArrayList<>();
        if (floatList != null && floatList.size() > 0) {
            int length=floatList.get(0).size();
            for (int i = 0; i < length; i++) {
                float sum=0.0f;
                for (int j = 0; j <floatList.size() ; j++) {
                    List<Float> floats = floatList.get(j);
                    sum+=floats.get(i);
                }
                resultList.add(sum);
            }
        }
        return resultList;
    }

    public static List<Float> strToListFloat(String data) {
        return JSONArray.parseArray(data, Float.class);
    }


    public static Map<String, NodeDataTimeline> parseNodeDataTimeLine(List<CnNodeData> nodeDataList) {
        Map<String, NodeDataTimeline> timelineMap = new HashMap<>();
        if (nodeDataList != null && nodeDataList.size() > 0) {
            for (int i = 0; i < nodeDataList.size(); i++) {
                CnNodeData cnNodeData = nodeDataList.get(i);
                String nodeId = cnNodeData.getNodeId().toString();
                if(timelineMap.get(nodeId)!=null)
                {
                    continue;
                }
                NodeDataTimeline nodeDataTimeline = new NodeDataTimeline();
                String data = cnNodeData.getData();
                List<Float> floats = strToListFloat(data);
                List<String> times = new ArrayList<>();
                for (int j = 0; j < floats.size(); j++) {
                    times.add(DateTimeUtil.getTime(cnNodeData.getStartDate(), j));
                }
                List<List<Float>> dataList=new ArrayList<>();
                for (int j = 0; j < nodeDataList.size() ; j++) {
                    String disNode = nodeDataList.get(j).getNodeId().toString();
                    if(nodeId.equals(disNode))
                    {
                        dataList.add(strToListFloat(nodeDataList.get(j).getData()));
                    }
                }
                nodeDataTimeline.setData(disSumFloatList(dataList));
                nodeDataTimeline.setTimes(times);

                timelineMap.put(nodeId, nodeDataTimeline);
            }
        }
        return timelineMap;
    }

    public static void main(String[] args) {
        List<Float> a =new ArrayList<>();
        a.add(1.1f);
        a.add(2.2f);
        a.add(3.3f);
        List<Float> b =new ArrayList<>();
        b.add(1.1f);
        b.add(2.2f);
        b.add(3.3f);
        List<List<Float>> lists=new ArrayList<>();
        lists.add(a);
        lists.add(b);
        System.out.println(disSumFloatList(lists));

    }

}
